package com.haoshuang.zhouzhoubang.ui.page.settled.collaborationcosts

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityCollaborationCostsBinding
import com.haoshuang.zhouzhoubang.model.bean.StoreYearMoneyBean
import com.haoshuang.zhouzhoubang.ui.page.settled.pay.AnnualFeePayActivity
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.ImageViewerPopupView
import com.lxj.xpopup.interfaces.OnSrcViewUpdateListener
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.adapter
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.vertical
import com.mufeng.libs.utils.view.visibleOrGone


/**
 *  合作费用
 */
class CollaborationCostsActivity :
    BaseVMActivity<CollaborationCostsViewModel, ActivityCollaborationCostsBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<CollaborationCostsActivity>()
        }
    }

    private var yearMoney: StoreYearMoneyBean? = null
    private val data = arrayListOf<StoreYearMoneyBean.Welfare>()
    private val adapter = CollaborationCostsAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "合作费用"
        binding.titleBar.leftImageView().click { finish() }

        binding.rvPlatformBenefits.vertical()
            .adapter(adapter)

        binding.tvPay.click {
            // 缴纳合作费用
            startPage<AnnualFeePayActivity>(
                "year" to yearMoney?.service_year,
                "fee" to "${yearMoney?.join_money}"
            )
        }

        binding.tvLookTeamHead.click {
            val list = arrayListOf<String>()
            list.add(yearMoney?.team_logo ?: "")
            list.add(yearMoney?.team_side_logo ?: "")
            list.add(yearMoney?.team_right_logo ?: "")
            XPopup.Builder(mActivity)
                .asImageViewer(binding.rivStoreHeadPic, 0,
                    list as List<Any>?,
                    { popupView, position ->
                        popupView.updateSrcView(if (position == 0) binding.rivStoreHeadPic else if (position == 1) binding.rivStoreHeadPicLeft else binding.rivStoreHeadPicRight)
                    }, SmartGlideImageLoader()
                )
                .show()
        }

        binding.tvLookBusinessLicense.click {
            XPopup.Builder(mActivity)
                .asImageViewer(
                    binding.rivBusinessLicense,
                    yearMoney?.team_company_pic,
                    SmartGlideImageLoader()
                )
                .show()
        }

    }

    override fun initData() {
        vm.getYearMoney()
    }

    override fun startObserve() {

        vm.getYearMoneyData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                yearMoney = it?.data
                if (it?.data != null) {
                    setupData(it.data!!)
                }
            }
        }

    }

    private fun setupData(bean: StoreYearMoneyBean) {
        // 合作年限
        binding.tvTimeLimit.text = "${bean.service_year}年"
        // 门店年费
        binding.tvPrice.text = "￥${bean.join_money}"
        // 创建时间
        binding.tvCreateTime.text = bean.add_time
        // 到期时间
        binding.tvEndTime.text = bean.end_time

        // 平台福利
        data.clear()
        data.addAll(bean.welfare ?: emptyList())
        adapter.notifyDataSetChanged()
        binding.llPlatformGift.visibleOrGone(data.isNotEmpty())

        // 门店信息
        binding.tvStoreName.text = bean.team_name
        // 门店头像
        binding.rivStoreHeadPic.load(bean.team_logo)
        binding.rivStoreHeadPicRight.load(bean.team_right_logo)
        binding.rivStoreHeadPicLeft.load(bean.team_side_logo)
        // 门店地址
        binding.tvStoreAddress.text = "${bean.province}${bean.city}${bean.area}${bean.address}"
        // 门店营业执照
        binding.rivBusinessLicense.load(bean.team_company_pic)
    }

}